﻿using System;
using System.Collections.Generic;
using System.Linq;

using HelfenHelfen.DataAccess.Entities;
using HelfenHelfen.DataAccess.Interfaces;

namespace HelfenHelfen.DataAccess
{
    public class DemandDataAccess : IDemandDataAccess
    {
        public void Delete(Guid demandId)
        {
            throw new NotImplementedException();
        }


        public Demand Get(Guid demandId)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                return (from demand in db.Demands
                        where demand.DemandGUID == demandId
                        select demand).FirstOrDefault();
            }
        }


        public IList<Demand> GetAll(int first, string city, Guid category, DateTime? date)
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                IQueryable<Demand> query;
                query = db.Demands;

                if (!string.IsNullOrEmpty(city))
                    query = query.Where(demand => demand.City == city);
                if (category != Guid.Empty)
                    query = query.Where(demand => demand.DemandCategoryGUID == category);
                if (date.HasValue)
                {
                    DateTime nextDay = date.Value.AddDays(1);

                    query = query.Where(demand =>
                        (demand.StartDateTime <= date.Value && demand.EndDateTime >= date.Value )
                        || demand.StartDateTime >= date.Value && demand.StartDateTime <= nextDay);
                }
                return query.Take(first).ToList();
            }
        }


        public IList<string> GetAllCities()
        {
            using (HelfenHelfenEntities db = new HelfenHelfenEntities())
            {
                return (from demand in db.Demands
                        select demand.City).Distinct().ToList();
            }
        }


        public void Insert(Demand newDemand)
        {
            throw new NotImplementedException();
        }


        public void Update(Demand updateDemand)
        {
            throw new NotImplementedException();
        }
    }
}
